
import http, { baseParams } from "@/utils/request";
import { UrlSaveFaceInfo, UrlSaveOcrInfo } from "@/utils/apiUrl";
import { Toast } from "antd-mobile";
import { getZoneRealTime } from "./zone";
import { setZoneArea } from "./getZoneArea";

//类型：请求成功
export const SAVE_FACE_REQ_SUCCESS = "SAVE_FACE_REQ_SUCCESS"

//类型：请求失败
export const SAVE_FACE_REQ_FAIL = "SAVE_FACE_REQ_FAIL"

/**
 * 保存人脸信息
 * @returns thunk
 */
export const saveFace = (imgUrl, resCallback) => {
    return async (dispatch) => {
        getZoneRealTime(async (result, zonePath) => {
            // 更新状态
            dispatch(setZoneArea(zonePath));

            //组装参数
            const params = baseParams()
            params.faceImgUrl = imgUrl
            params.cardType = ""

            //发起请求
            await http.post(zonePath + UrlSaveFaceInfo, params)
                .then((response) => {
                    // 处理上传成功的响应
                    const resCode = parseInt(response.code)
                    if (resCode === 0) {
                        console.log('=======人脸保存成功');
                        console.log(response);
                        resCallback(true)
                    } else {
                        Toast.show(response.msg || "")
                        resCallback(false)
                    }
                })
                .catch((error) => {
                    // 处理上传失败和网络错误
                    console.error('=======人脸保存失败', error);
                    Toast.show(error)
                    resCallback(false)
                });
        });
    }
}


/**
 * 保存OCR信息
 * @returns thunk
 */
export const saveOcr = (cardImgUrl, cardBackImgUrl, cardType, resCallback) => {
    return async (dispatch) => {
        getZoneRealTime(async (result, zonePath) => {
            // 更新状态
            dispatch(setZoneArea(zonePath));

            //组装参数
            const params = baseParams()
            params.cardImgUrl = cardImgUrl
            params.cardBackImgUrl = cardBackImgUrl
            params.cardType = cardType

            //发起请求
            await http.post(zonePath + UrlSaveOcrInfo, params)
                .then((response) => {
                    // 处理上传成功的响应
                    const resCode = parseInt(response.code)
                    if (resCode === 0) {
                        console.log('=======ocr保存成功');
                        console.log(response);
                        resCallback(true)
                    } else {
                        Toast.show(response.msg || "")
                        resCallback(false)
                    }
                })
                .catch((error) => {
                    // 处理上传失败和网络错误
                    console.error('=======ocr保存失败', error);
                    Toast.show(error)
                    resCallback(false)
                });
        });
    }
}
